package com.white.hot.controller.umpush;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.white.hot.controller.umpush.android.AndroidBroadcast;
import com.white.hot.controller.umpush.android.AndroidCustomizedcast;
import com.white.hot.controller.umpush.android.AndroidFilecast;
import com.white.hot.controller.umpush.android.AndroidGroupcast;
import com.white.hot.controller.umpush.android.AndroidUnicast;

public class Demo
{

	private String appkey = null;
	private String appMasterSecret = null;
	private String timestamp = null;
	private PushClient client = new PushClient();

	public Demo(String key, String secret)
	{
		try
		{
			appkey = key;
			appMasterSecret = secret;
		} catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void sendAndroidBroadcast() throws Exception
	{
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,
				appMasterSecret);
		// 必填 通知栏提示文字
		broadcast.setTicker("Android broadcast ticker");
		// 必填 通知标题
		broadcast.setTitle("中文的title");
		// 必填 通知文字描述
		broadcast.setText("Android broadcast text");
		// 点击之后的后续行为
		broadcast.goAppAfterOpen();
		// 发送通知还是消息
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		// 可选 true/false 正式/测试模式。测试模式下，只会将消息发给测试设备。
		broadcast.setProductionMode();
		// Set customized fields
		/*
		 * 可选 用户自定义key-value。只对"通知(display_type=notification)"生效。 可以配合通知到达后,
		 * 打开App, 打开URL, 打开Activity使用。 注意: SDK V1.2.3后开始支持
		 */
		broadcast.setExtraField("test", "helloworld");
		client.send(broadcast);
	}

	public void sendAndroidUnicast() throws Exception
	{
		AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken("AhiPYm4SnVlCYYd7gK9AbN5EVxJJI6agb4MwJ_BELWj_");
		unicast.setTicker("体操课Android unicast ticker");
		unicast.setTitle("标题");
		unicast.setText("内容Android unicast text");
		// unicast.goAppAfterOpen();
		unicast.goActivityAfterOpen("com.zkingsoft.readapp.activities.PersonalMessageActivity");
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		unicast.setProductionMode();
		// Set customized fields
		unicast.setExtraField("test", "helloworld");
		client.send(unicast);
	}

	public void sendAndroidGroupcast() throws Exception
	{
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,
				appMasterSecret);
		/*
		 * TODO Construct the filter condition: "where": { "and": [
		 * {"tag":"test"}, {"tag":"Test"} ] }
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		JSONObject TestTag = new JSONObject();
		testTag.put("tag", "test");
		TestTag.put("tag", "2");
		// tagArray.put(testTag);
		tagArray.add(TestTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		// 可选 终端用户筛选条件,如用户标签、地域、应用版本以及渠道等,详细请参考附录G。
		groupcast.setFilter(filterJson);
		groupcast.setTicker("组播Android groupcast ticker");
		groupcast.setTitle("组播标题中文的title");
		groupcast.setText("组播内容Android groupcast text");
		groupcast.goAppAfterOpen();
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		groupcast.setProductionMode();
		client.send(groupcast);
	}

	public void sendAndroidCustomizedcast() throws Exception
	{
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(
				appkey, appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are
		// multiple alias.
		// And if you have many alias, you can also upload a file containing
		// these alias, then
		// use file_id to send customized notification.
		/*
		 * "alias":"xx", // 可选
		 * 当type=customizedcast时,开发者填写自己的alias,友盟根据alias进行反查找,得到对应的device_token。
		 * "alias_type": "xx" // 可选 当type=customizedcast时,必填。 表示alias的类型。
		 */
		customizedcast.setAlias("alias", "alias_type");
		customizedcast.setTicker("Android customizedcast ticker");
		customizedcast.setTitle("中文的title");
		customizedcast.setText("Android customizedcast text");
		customizedcast.goAppAfterOpen();
		customizedcast
				.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		customizedcast.setProductionMode();
		client.send(customizedcast);
	}

	public void sendAndroidCustomizedcastFile() throws Exception
	{
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(
				appkey, appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are
		// multiple alias.
		// And if you have many alias, you can also upload a file containing
		// these alias, then
		// use file_id to send customized notification.
		String fileId = client.uploadContents(appkey, appMasterSecret, "aa"
				+ "\n" + "bb" + "\n" + "alias");
		customizedcast.setFileId(fileId, "alias_type");
		customizedcast.setTicker("Android customizedcast ticker");
		customizedcast.setTitle("中文的title");
		customizedcast.setText("Android customizedcast text");
		customizedcast.goAppAfterOpen();
		customizedcast
				.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		customizedcast.setProductionMode();
		client.send(customizedcast);
	}

	public void sendAndroidFilecast() throws Exception
	{
		AndroidFilecast filecast = new AndroidFilecast(appkey, appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there
		// are multiple tokens
		String fileId = client.uploadContents(appkey, appMasterSecret, "aa"
				+ "\n" + "bb");
		filecast.setFileId(fileId);
		filecast.setTicker("Android filecast ticker");
		filecast.setTitle("中文的title");
		filecast.setText("Android filecast text");
		filecast.goAppAfterOpen();
		filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		client.send(filecast);
	}

	public static void main(String[] args)
	{
		// TODO set your appkey and master secret here
		Demo demo = new Demo("5822bcf38630f518eb0036a6",
				"2xnayicvmy36cvk0iiqmvslgkjl9uvfi");
		try
		{
			// demo.sendAndroidBroadcast();
			demo.sendAndroidUnicast();
			// demo.sendAndroidGroupcast();
			/*
			 * TODO these methods are all available, just fill in some fields
			 * and do the test demo.sendAndroidCustomizedcastFile();
			 * demo.sendAndroidBroadcast(); demo.sendAndroidGroupcast();
			 * demo.sendAndroidCustomizedcast(); demo.sendAndroidFilecast();
			 * 
			 * demo.sendIOSBroadcast(); demo.sendIOSUnicast();
			 * demo.sendIOSGroupcast(); demo.sendIOSCustomizedcast();
			 * demo.sendIOSFilecast();
			 */
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	/*
	 * 附录G 目前开放的筛选字段有: "app_version"(应用版本), "channel"(分发渠道),
	 * "device_model"(设备型号), "province"(省)，"tag"(用户标签), "country"(国家),
	 * "language"(语言) 。 "country"和"province"的类型定义请参照 附录J 注意: Query
	 * expression的最顶层只能是 and 或者 or and条件示例 已注册的(registered_user) 并且 为男性(male) 并且
	 * 版本(app_version)是1.2.0的用户群 "where": { "and": [ {"tag":"registered_user"},
	 * {"tag":"male"}, {"app_version":"1.2.0"} ] }
	 * 
	 * or条件示例 已注册(registered_user)用户 或者 男性(male) 或者 北京市的用户群 "where": { "or": [
	 * {"tag":"registered_user"}, {"tag":"male"}, {"province":"北京"} ] }
	 * 
	 * not条件示例 未注册的(registered_user)的用户群 "where": { "and": [ { "not": { "tag":
	 * "registered_user" } } ] }
	 * 
	 * 注意: Query的最外层必须是and或者or，not当前不支持。 and, or, not组合条件示例
	 * 发送给" 非 注册 或者 男性(male)" 并且 "玩过5次以上的用户(played_more_than_5_times)" "where":
	 * { "and": [ { "or": [ { "not": { "tag": "registered_user" } }, { "tag":
	 * "male" } ] }, { "tag": "played_more_than_5_times" } ] }
	 */
}
